package com.zzyl.nursing.controller;

import cn.hutool.json.JSONObject;
import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.nursing.domain.Device;
import com.zzyl.nursing.dto.ChangeDeviceDTO;
import com.zzyl.nursing.dto.DeviceDTO;
import com.zzyl.nursing.dto.DeviceDetailDTO;
import com.zzyl.nursing.service.IDeviceService;
import com.zzyl.nursing.vo.DeviceDetailVo;
import com.zzyl.nursing.vo.DeviceVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 设备表Controller
 *
 * @author mr.luo
 * @date 2025-05-25
 */
@RestController
@RequestMapping("/nursing/device")
@Api(tags = "设备表相关接口")
public class DeviceController extends BaseController {
    @Autowired
    private IDeviceService deviceService;

    /**
     * 同步产品列表
     */
    @PostMapping("syncProductList")
    @ApiOperation(value = "同步产品列表")
    public AjaxResult syncProductList() {
        deviceService.syncProductList();
        return success();
    }

    /**
     * 1.2 查询所有产品列表
     */
    @GetMapping("allProduct")
    @ApiOperation(value = "查询所有产品列表")
    public AjaxResult allProduct() {
        List<DeviceVO> list = deviceService.allProduct();
        return success(list);
    }

    /**
     * 查询设备表列表
     */
    @PreAuthorize("@ss.hasPermi('nursing:device:list')")
    @GetMapping("/list")
    @ApiOperation(value = "获取设备表列表")
    public TableDataInfo<List<Device>> list(Device device) {
        startPage();
        List<Device> list = deviceService.selectDeviceList(device);
        return getDataTable(list);
    }


    /**
     * 注册设备
     */
    @PostMapping("/register")
    @ApiOperation("注册设备")
    public AjaxResult register(@RequestBody DeviceDTO deviceDTO) {
        deviceService.register(deviceDTO);
        return success();
    }

    /**
     * 1.5 查询设备详细数据
     */
    @PostMapping("/queryDeviceDetail")
    @ApiOperation("查询设备详细数据")
    public AjaxResult getDeviceDetail(@RequestBody DeviceDetailDTO deviceDetailDTO) {
        DeviceDetailVo deviceDetailVo = deviceService.getDeviceDetail(deviceDetailDTO);
        return success(deviceDetailVo);
    }

    /**
     * 1.6 查看指定产品的已发布物模型中的功能定义详情
     */
    @PostMapping("/queryProductModelInfo")
    @ApiOperation("查看指定产品的已发布物模型中的功能定义详情")
    public AjaxResult getProductDetail(@RequestBody Device device) {
        JSONObject jobDetail = deviceService.getProductDetail(device.getProductKey());
        return success(jobDetail);
    }

    /**
     * 1.7 查询指定设备的物模型运行状态
     */
    @PostMapping("/queryDevicePropertyStatus")
    @ApiOperation("查询指定设备的物模型运行状态")
    public AjaxResult getDeviceStatus(@RequestBody DeviceDTO deviceDTO) {
        Map<String, Object> map = deviceService.getDeviceStatus(deviceDTO);
        return success(map);
    }

    /**
     * 1.8 修改设备备注名称
     */
    @PutMapping
    @ApiOperation("修改设备备注名称")
    public AjaxResult changeDevice(@RequestBody ChangeDeviceDTO changeDeviceDTO){
        deviceService.changeDevice(changeDeviceDTO);
        return success();
    }

    /**
     * 删除设备
     */
    @DeleteMapping
    @ApiOperation("删除设备")
    public AjaxResult remove(@RequestBody DeviceDetailDTO deviceDetailDTO) {
        deviceService.deleteDevice(deviceDetailDTO);
        return success();
    }

}